{
"cells": [
{
"cell_type": "markdown",
"id": "71fbe41c",
"metadata": {},
"source": [
"# Configuring Parallelization\n",
"\n",
"**PyBroker** uses [Joblib](https://joblib.readthedocs.io/) to compute indicators, train models, and optimize parameters in parallel."
]
},
{
"cell_type": "markdown",
"id": "3c6d4ee0",
"metadata": {},
"source": [
"## Setting Workers\n",
"\n",
"[set_parallel](https://www.pybroker.com/en/latest/reference/pybroker.parallel.html#pybroker.parallel.set_parallel) updates the global Joblib configuration. The [n_jobs](https://www.pybroker.com/en/latest/reference/pybroker.parallel.html#pybroker.parallel.ParallelConfig.n_jobs) parameter specifies the number of worker jobs: `-1` (the default) uses all available CPU cores, and `1` runs sequentially. Read the current settings with [get_parallel_config](https://www.pybroker.com/en/latest/reference/pybroker.parallel.html#pybroker.parallel.get_parallel_config):"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "cbde08ed",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-11T20:30:23.146205Z",
"iopub.status.busy": "2026-08-11T20:30:23.146027Z",
"iopub.status.idle": "2026-08-11T20:30:23.757584Z",
"shell.execute_reply": "2026-08-11T20:30:23.757082Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ParallelConfig(n_jobs=4, backend='loky', parallel=None)\n",
"ParallelConfig(n_jobs=1, backend='loky', parallel=None)\n"
]
}
],
"source": [
"from pybroker import set_parallel, get_parallel_config\n",
"\n",
"# Use a fixed number of workers.\n",
"set_parallel(n_jobs=4)\n",
"print(get_parallel_config())\n",
"\n",
"# Or disable parallel execution entirely.\n",
"set_parallel(n_jobs=1)\n",
"print(get_parallel_config())"
]
},
{
"cell_type": "markdown",
"id": "875ccfcc",
"metadata": {},
"source": [
"## Parallel Indicators\n",
"\n",
"Indicators are computed **per symbol**: all indicators for a given symbol are grouped into a single task, and one task is dispatched per symbol. Passing `parallel_indicators=True` to [backtest](https://www.pybroker.com/en/latest/reference/pybroker.strategy.html#pybroker.strategy.Strategy.backtest), [walkforward](https://www.pybroker.com/en/latest/reference/pybroker.strategy.html#pybroker.strategy.Strategy.walkforward), or [optimize](https://www.pybroker.com/en/latest/reference/pybroker.strategy.html#pybroker.strategy.Strategy.optimize) runs these tasks across the configured workers (defaulting to `False`).\n",
"\n",
"To see this in action, let's backtest a moving average crossover:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "afca8206",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-11T20:30:23.758798Z",
"iopub.status.busy": "2026-08-11T20:30:23.758558Z",
"iopub.status.idle": "2026-08-11T20:30:25.970161Z",
"shell.execute_reply": "2026-08-11T20:30:25.969710Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Backtesting: 2021-01-01 00:00:00 to 2026-01-01 00:00:00\n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loading bar data...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
"[ 0% ]"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
"[**********************67%******* ] 2 of 3 completed"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
"[*********************100%***********************] 3 of 3 completed"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loaded bar data: 0:00:00 \n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Computing indicators...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
"\u001b[38;2;255;0;0m 0%\u001b[39m \u001b[38;2;255;0;0m(0 of 3)\u001b[39m | | Elapsed Time: 0:00:00 ETA: --:--:--"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
"\u001b[38;2;255;148;0m 33%\u001b[39m \u001b[38;2;255;148;0m(1 of 3)\u001b[39m |######## | Elapsed Time: 0:00:01 ETA: 0:00:02"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
"\u001b[38;2;0;255;0m100%\u001b[39m \u001b[38;2;0;255;0m(3 of 3)\u001b[39m |##########################| Elapsed Time: 0:00:01 Time: 0:00:01"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test split: 2021-01-04 00:00:00 to 2025-12-31 00:00:00\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
"\u001b[38;2;255;0;0m 0%\u001b[39m \u001b[38;2;255;0;0m(0 of 1255)\u001b[39m | | Elapsed Time: 0:00:00 ETA: --:--:--"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
"\u001b[38;2;255;261;0m 58%\u001b[39m \u001b[38;2;255;261;0m(731 of 1255)\u001b[39m |############ | Elapsed Time: 0:00:00 ETA: 0:00:00"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
"\u001b[38;2;0;255;0m100%\u001b[39m \u001b[38;2;0;255;0m(1255 of 1255)\u001b[39m |####################| Elapsed Time: 0:00:00 Time: 0:00:00"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Finished backtest: 0:00:02\n"
]
},
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" name | \n",
" value | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" trade_count | \n",
" 218 | \n",
"
\n",
" \n",
" | 1 | \n",
" initial_market_value | \n",
" 100000.0 | \n",
"
\n",
" \n",
" | 2 | \n",
" end_market_value | \n",
" 116119.9 | \n",
"
\n",
" \n",
" | 3 | \n",
" total_pnl | \n",
" 15837.68 | \n",
"
\n",
" \n",
" | 4 | \n",
" unrealized_pnl | \n",
" 282.22 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" name value\n",
"0 trade_count 218\n",
"1 initial_market_value 100000.0\n",
"2 end_market_value 116119.9\n",
"3 total_pnl 15837.68\n",
"4 unrealized_pnl 282.22"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"import pybroker\n",
"from pybroker import Strategy, YFinance, sumv\n",
"\n",
"pybroker.enable_data_source_cache(\"parallelization\")\n",
"set_parallel(n_jobs=-1)\n",
"\n",
"\n",
"def sma(bar_data, period):\n",
" return sumv(bar_data.close, period) / period\n",
"\n",
"\n",
"sma_20 = pybroker.indicator(\"sma_20\", sma, period=20)\n",
"\n",
"\n",
"def sma_cross(ctx):\n",
" sma_vals = ctx.indicator(\"sma_20\")\n",
" if np.isnan(sma_vals[-1]):\n",
" return\n",
" pos = ctx.long_pos()\n",
" if not pos and ctx.close[-1] > sma_vals[-1]:\n",
" ctx.buy_shares = 100\n",
" elif pos and ctx.close[-1] < sma_vals[-1]:\n",
" ctx.sell_all_shares()\n",
"\n",
"\n",
"yfinance = YFinance()\n",
"strategy = Strategy(yfinance, start_date=\"1/1/2021\", end_date=\"1/1/2026\")\n",
"strategy.add_execution(sma_cross, [\"V\", \"MA\", \"AXP\"], indicators=sma_20)\n",
"\n",
"result = strategy.backtest(parallel_indicators=True, warmup=20)\n",
"result.metrics_df.head()"
]
},
{
"cell_type": "markdown",
"id": "8abd5633",
"metadata": {},
"source": [
"Standalone indicator computation with an [IndicatorSet](https://www.pybroker.com/en/latest/reference/pybroker.indicator.html#pybroker.indicator.IndicatorSet) accepts the same `parallel_indicators` flag:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "a571b920",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-11T20:30:25.971385Z",
"iopub.status.busy": "2026-08-11T20:30:25.971273Z",
"iopub.status.idle": "2026-08-11T20:30:27.174595Z",
"shell.execute_reply": "2026-08-11T20:30:27.174044Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loaded cached bar data.\n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Computing indicators...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
"\u001b[38;2;255;0;0m 0%\u001b[39m \u001b[38;2;255;0;0m(0 of 3)\u001b[39m | | Elapsed Time: 0:00:00 ETA: --:--:--"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
"\u001b[38;2;255;148;0m 33%\u001b[39m \u001b[38;2;255;148;0m(1 of 3)\u001b[39m |######## | Elapsed Time: 0:00:01 ETA: 0:00:02"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
"\u001b[38;2;0;255;0m100%\u001b[39m \u001b[38;2;0;255;0m(3 of 3)\u001b[39m |##########################| Elapsed Time: 0:00:01 Time: 0:00:01"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" symbol | \n",
" date | \n",
" sma_20 | \n",
"
\n",
" \n",
" \n",
" \n",
" | 3760 | \n",
" V | \n",
" 2025-12-24 | \n",
" 339.050002 | \n",
"
\n",
" \n",
" | 3761 | \n",
" V | \n",
" 2025-12-26 | \n",
" 340.110501 | \n",
"
\n",
" \n",
" | 3762 | \n",
" V | \n",
" 2025-12-29 | \n",
" 341.119000 | \n",
"
\n",
" \n",
" | 3763 | \n",
" V | \n",
" 2025-12-30 | \n",
" 342.280499 | \n",
"
\n",
" \n",
" | 3764 | \n",
" V | \n",
" 2025-12-31 | \n",
" 343.334999 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" symbol date sma_20\n",
"3760 V 2025-12-24 339.050002\n",
"3761 V 2025-12-26 340.110501\n",
"3762 V 2025-12-29 341.119000\n",
"3763 V 2025-12-30 342.280499\n",
"3764 V 2025-12-31 343.334999"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from pybroker import IndicatorSet\n",
"\n",
"df = yfinance.query(\n",
" [\"V\", \"MA\", \"AXP\"], start_date=\"1/1/2021\", end_date=\"1/1/2026\"\n",
")\n",
"ind_set = IndicatorSet()\n",
"ind_set.add(sma_20)\n",
"ind_set(df, parallel_indicators=True).tail()"
]
},
{
"cell_type": "markdown",
"id": "0a881b12",
"metadata": {},
"source": [
"## Parallel Model Training\n",
"\n",
"Model training runs serially by default. Passing `parallel_models=True` to [backtest](https://www.pybroker.com/en/latest/reference/pybroker.strategy.html#pybroker.strategy.Strategy.backtest) or [walkforward](https://www.pybroker.com/en/latest/reference/pybroker.strategy.html#pybroker.strategy.Strategy.walkforward) trains each model in its own task across the configured workers.\n",
"\n",
"This example adapts the linear regression model from [Training a Model](https://www.pybroker.com/en/latest/notebooks/6.%20Training%20a%20Model.html):"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "20855b93",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-11T20:30:27.175850Z",
"iopub.status.busy": "2026-08-11T20:30:27.175731Z",
"iopub.status.idle": "2026-08-11T20:30:30.198076Z",
"shell.execute_reply": "2026-08-11T20:30:30.197554Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Backtesting: 2021-01-01 00:00:00 to 2026-01-01 00:00:00\n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loaded cached bar data.\n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Computing indicators...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
"\u001b[38;2;255;0;0m 0%\u001b[39m \u001b[38;2;255;0;0m(0 of 3)\u001b[39m | | Elapsed Time: 0:00:00 ETA: --:--:--"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
"\u001b[38;2;255;148;0m 33%\u001b[39m \u001b[38;2;255;148;0m(1 of 3)\u001b[39m |######## | Elapsed Time: 0:00:00 ETA: 0:00:00"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
"\u001b[38;2;0;255;0m100%\u001b[39m \u001b[38;2;0;255;0m(3 of 3)\u001b[39m |##########################| Elapsed Time: 0:00:00 Time: 0:00:00"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train split: 2021-01-05 00:00:00 to 2022-08-31 00:00:00\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Finished training models: 0:00:01 \n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test split: 2022-09-01 00:00:00 to 2024-05-01 00:00:00\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
"\u001b[38;2;255;0;0m 0%\u001b[39m \u001b[38;2;255;0;0m(0 of 418)\u001b[39m | | Elapsed Time: 0:00:00 ETA: --:--:--"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
"\u001b[38;2;0;255;0m100%\u001b[39m \u001b[38;2;0;255;0m(418 of 418)\u001b[39m |######################| Elapsed Time: 0:00:00 Time: 0:00:00"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train split: 2022-09-01 00:00:00 to 2024-05-01 00:00:00\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Finished training models: 0:00:01 \n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test split: 2024-05-02 00:00:00 to 2025-12-31 00:00:00\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
"\u001b[38;2;255;0;0m 0%\u001b[39m \u001b[38;2;255;0;0m(0 of 418)\u001b[39m | | Elapsed Time: 0:00:00 ETA: --:--:--"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
"\u001b[38;2;0;255;0m100%\u001b[39m \u001b[38;2;0;255;0m(418 of 418)\u001b[39m |######################| Elapsed Time: 0:00:00 Time: 0:00:00"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Finished backtest: 0:00:03\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" name | \n",
" value | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" trade_count | \n",
" 52 | \n",
"
\n",
" \n",
" | 1 | \n",
" initial_market_value | \n",
" 100000.0 | \n",
"
\n",
" \n",
" | 2 | \n",
" end_market_value | \n",
" 146555.0 | \n",
"
\n",
" \n",
" | 3 | \n",
" total_pnl | \n",
" 16058.0 | \n",
"
\n",
" \n",
" | 4 | \n",
" unrealized_pnl | \n",
" 30497.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" name value\n",
"0 trade_count 52\n",
"1 initial_market_value 100000.0\n",
"2 end_market_value 146555.0\n",
"3 total_pnl 16058.0\n",
"4 unrealized_pnl 30497.0"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from sklearn.linear_model import LinearRegression\n",
"\n",
"from pybroker.indicator import close_minus_ma\n",
"\n",
"cmma_20 = close_minus_ma(\"cmma_20\", lookback=20, atr_length=20)\n",
"\n",
"\n",
"def train_slr(symbol, train_data, test_data):\n",
" # Predict the next bar's return given the 20-day CMMA.\n",
" prev_close = train_data[\"close\"].shift(1)\n",
" daily_returns = (train_data[\"close\"] - prev_close) / prev_close\n",
" train_data[\"pred\"] = daily_returns.shift(-1)\n",
" train_data = train_data.dropna()\n",
" model = LinearRegression()\n",
" model.fit(train_data[[\"cmma_20\"]], train_data[[\"pred\"]])\n",
" # Return the trained model and columns to use as input data.\n",
" return model, [\"cmma_20\"]\n",
"\n",
"\n",
"model_slr = pybroker.model(\"slr\", train_slr, indicators=[cmma_20])\n",
"\n",
"\n",
"def hold_long(ctx):\n",
" if not ctx.long_pos():\n",
" if ctx.preds(\"slr\")[-1] > 0:\n",
" ctx.buy_shares = 100\n",
" elif ctx.preds(\"slr\")[-1] < 0:\n",
" ctx.sell_all_shares()\n",
"\n",
"\n",
"model_strategy = Strategy(yfinance, start_date=\"1/1/2021\", end_date=\"1/1/2026\")\n",
"model_strategy.add_execution(hold_long, [\"V\", \"MA\", \"AXP\"], models=model_slr)\n",
"result = model_strategy.walkforward(\n",
" windows=2,\n",
" train_size=0.5,\n",
" lookahead=1,\n",
" warmup=20,\n",
" parallel_models=True,\n",
")\n",
"result.metrics_df.head()"
]
},
{
"cell_type": "markdown",
"id": "26231549",
"metadata": {},
"source": [
"## Using Ray as the Backend\n",
"\n",
"[Ray](https://www.ray.io/) can distribute the same work across many cores or an entire cluster. Install it with `pip install ray` and then register it with Joblib via [register_ray](https://docs.ray.io/en/latest/ray-more-libs/joblib.html) (from `ray.util.joblib`):"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "d52f6e4b",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-11T20:30:30.199565Z",
"iopub.status.busy": "2026-08-11T20:30:30.199385Z",
"iopub.status.idle": "2026-08-11T20:30:36.292609Z",
"shell.execute_reply": "2026-08-11T20:30:36.292088Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2026-08-11 13:30:35,114\tINFO worker.py:2024 -- Started a local Ray instance.\n"
]
}
],
"source": [
"import ray\n",
"from ray.util.joblib import register_ray\n",
"\n",
"ray.init(num_cpus=2, include_dashboard=False, ignore_reinit_error=True)\n",
"register_ray()"
]
},
{
"cell_type": "markdown",
"id": "3e18c099-d2de-4280-9878-5b9163603ec0",
"metadata": {},
"source": [
"After registering Ray, pass `backend=\"ray\"` to [set_parallel](https://www.pybroker.com/en/latest/reference/pybroker.parallel.html#pybroker.parallel.set_parallel) to make it available as a backend:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "7befd7b3-8314-450c-9025-5ad71fc46ebb",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-11T20:30:36.294109Z",
"iopub.status.busy": "2026-08-11T20:30:36.293932Z",
"iopub.status.idle": "2026-08-11T20:30:36.296051Z",
"shell.execute_reply": "2026-08-11T20:30:36.295640Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ParallelConfig(n_jobs=-1, backend='ray', parallel=None)\n"
]
}
],
"source": [
"set_parallel(backend=\"ray\", n_jobs=-1)\n",
"print(get_parallel_config())"
]
},
{
"cell_type": "markdown",
"id": "093a8c77-28a5-4b93-b475-ae2d5baaf164",
"metadata": {},
"source": [
"**PyBroker** will now use the Ray backend for all parallel tasks. For example, calling [backtest](https://www.pybroker.com/en/latest/reference/pybroker.strategy.html#pybroker.strategy.Strategy.backtest) with `parallel_indicators=True`:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "f4e4c1b9",
"metadata": {
"execution": {
"iopub.execute_input": "2026-08-11T20:30:36.296886Z",
"iopub.status.busy": "2026-08-11T20:30:36.296795Z",
"iopub.status.idle": "2026-08-11T20:30:39.807870Z",
"shell.execute_reply": "2026-08-11T20:30:39.807189Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Backtesting: 2021-01-01 00:00:00 to 2026-01-01 00:00:00\n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loaded cached bar data.\n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Computing indicators...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
"\u001b[38;2;255;0;0m 0%\u001b[39m \u001b[38;2;255;0;0m(0 of 3)\u001b[39m | | Elapsed Time: 0:00:00 ETA: --:--:--"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
"\u001b[38;2;255;148;0m 33%\u001b[39m \u001b[38;2;255;148;0m(1 of 3)\u001b[39m |######## | Elapsed Time: 0:00:02 ETA: 0:00:04"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
"\u001b[38;2;0;255;0m100%\u001b[39m \u001b[38;2;0;255;0m(3 of 3)\u001b[39m |##########################| Elapsed Time: 0:00:02 Time: 0:00:02"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test split: 2021-01-04 00:00:00 to 2025-12-31 00:00:00\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
"\u001b[38;2;255;0;0m 0%\u001b[39m \u001b[38;2;255;0;0m(0 of 1255)\u001b[39m | | Elapsed Time: 0:00:00 ETA: --:--:--"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
"\u001b[38;2;101;255;0m 91%\u001b[39m \u001b[38;2;101;255;0m(1151 of 1255)\u001b[39m |################## | Elapsed Time: 0:00:00 ETA: 0:00:00"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r",
"\u001b[38;2;0;255;0m100%\u001b[39m \u001b[38;2;0;255;0m(1255 of 1255)\u001b[39m |####################| Elapsed Time: 0:00:00 Time: 0:00:00"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Finished backtest: 0:00:02\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total return: 15.84%\n"
]
}
],
"source": [
"result = strategy.backtest(parallel_indicators=True, warmup=20)\n",
"print(f\"Total return: {result.metrics.total_return_pct:.2f}%\")\n",
"\n",
"ray.shutdown()"
]
},
{
"cell_type": "markdown",
"id": "3fe6a3b1-5e88-4ed3-84af-9c3eef622866",
"metadata": {},
"source": [
"We will explore [parameter optimization](https://www.pybroker.com/en/latest/notebooks/12.%20Parameter%20Optimization.html) in the next notebook, which can also be parallelized in certain scenarios."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}